package org.linlinjava.litemall.admin.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallAd;
import org.linlinjava.litemall.db.domain.LitemallBanner;
import org.linlinjava.litemall.db.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/admin/banner")
@Validated
@Api(value = "banner", tags = "banner接口")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class AdminBannerController {
    
    private final Log logger = LogFactory.getLog(AdminBannerController.class);

    private final BannerService bannerService;

    @GetMapping("/list")
    @ApiOperation("banner列表查询")
    public Object list(String name, String content,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallBanner> adList = bannerService.querySelective(name, content, page, limit, sort, order);
        return ResponseUtil.okList(adList);
    }

    private Object validate(LitemallBanner banner) {
        Integer goodsId = banner.getGoodId();
        if (StringUtils.isEmpty(goodsId)) {
            return ResponseUtil.badArgument();
        }
        String picUrl = banner.getPicUrl();
        if (StringUtils.isEmpty(picUrl)) {
            return ResponseUtil.badArgument();
        }
        return null;
    }

    @PostMapping("/create")
    @ApiOperation("新增banner")
    public Object create(@RequestBody LitemallBanner banner) {
        Object error = validate(banner);
        if (error != null) {
            return error;
        }
        bannerService.add(banner);
        return ResponseUtil.ok(banner);
    }

    @GetMapping("/read")
    @ApiOperation("查询banner")
    public Object read(@NotNull Integer id) {
        LitemallBanner ad = bannerService.findById(id);
        return ResponseUtil.ok(ad);
    }

    @PostMapping("/update")
    @ApiOperation("更新banner")
    public Object update(@RequestBody LitemallBanner banner) {
        Object error = validate(banner);
        if (error != null) {
            return error;
        }
        if (bannerService.updateById(banner) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        return ResponseUtil.ok(banner);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除banner")
    public Object delete(@PathVariable Integer id) {
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        bannerService.deleteById(id);
        return ResponseUtil.ok();
    }

}
